const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const LANGUAGE = process.env.LANGUAGE || 'en';

const PATHS = {
  app: path.join(__dirname, 'app'),
  temp: path.join(__dirname, 'temp'),
  build: path.join(__dirname, 'build/generate.jsx'),
};

module.exports = {
  entry: PATHS.build,
  output: {
    path: PATHS.temp,
    filename: 'generate.js',
    libraryTarget: 'commonjs2'
  },
  target: 'node',
  externals: fs.readdirSync('node_modules'),
  resolve: {
    extensions: ['.js', '.jsx']
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        use: ['babel-loader', {
          loader: 'ejs-html-loader',
          query:{
            UIText: require(`./app/lang/app_${LANGUAGE}.js`),
            LANGUAGE,
          }
        }],
        include: [PATHS.app, PATHS.build],
      },
      {
        test: /\.json$/,
        use: ['json-loader']
      },
      {
        test: /\.sass$/,
        use: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader', 'sass-loader?indentedSyntax=true']
        }),
        include: PATHS.app
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,
        use: ['file-loader?name=img/[hash].[ext]', 'img-loader?-minimize']
      }
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      "process.env": {
        NODE_ENV: JSON.stringify("production"),
        LANGUAGE: JSON.stringify(LANGUAGE),
      }
    }),
    new ExtractTextPlugin("css/[chunkhash].css"),
    // new webpack.optimize.UglifyJsPlugin({
    //   compress: {warnings: false}
    // })
  ]
};
